package com.example.javascaffold.utils;



import com.example.javascaffold.constant.Constant;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;


/**
 * <p><strong>图表结果格式化</strong></p><br>
 * <p>
 *     将结果映射成前端需要的图表数据格式  <br>
 * </p>
 * <br>
 * <li>
 * <p>
 *     <strong>示例1: 返回字段和值(常用于条形图)</strong> <br>
 *      dataList.add(ChartsUtils.getChartsFormatData(图表字段, 对应的值)); <br>
 *      返回结果: {"name": "xx", "number": "xx"} <br>
 * </p>
 * </li>
 * <li>
 * <p>
 *     <strong>示例2: 返回字段、值、所占百分比(常用于扇形图)</strong> <br>
 *      dataList.add(ChartsUtils.getChartsFormatData(图表字段, 对应的值, 总数的值)); <br>
 *      返回结果: {"name": "xx", "number": "xx", "percent": "xx"} <br>
 * </p>
 * </li>
 * @author song
 * @date 19:31 2021/5/10
 * @param 
 * @return 
 * @other
**/
public class ChartsUtils {

    private ChartsUtils() {}

    /**
     * <li>
     * <p>
     *     <strong>示例1: 返回字段和值(常用于条形图)</strong> <br>
     *      dataList.add(ChartsUtils.getChartsFormatData(图表字段, 对应的值)); <br>
     *      返回结果: {"name": "xx", "number": "xx"} <br>
     * </p>
     * </li>
    **/
    public static Map<String, Object> getChartsFormatData (String name, Integer number) {
        Map<String, Object> resultMap = new TreeMap<>();
        resultMap.put(Constant.DEFAULT_CHARTS_NAME, name);
        resultMap.put(Constant.DEFAULT_CHARTS_NUMBER, number);
        return resultMap;
    }

    /**
     * <li>
     * <p>
     *     <strong>示例2: 返回字段、值、所占百分比(常用于扇形图)</strong> <br>
     *      dataList.add(ChartsUtils.getChartsFormatData(图表字段, 对应的值, 总数的值 整数型)); <br>
     *      返回结果: {"name": "xx", "number": "xx", "percent": "xx"} <br>
     * </p>
     * </li>
    **/
    public static Map<String, Object> getChartsFormatData (String name, Integer number, Integer allNumber) {
        Map<String, Object> resultMap = new TreeMap<>();
        resultMap.put(Constant.DEFAULT_CHARTS_NAME, name);
        resultMap.put(Constant.DEFAULT_CHARTS_NUMBER, number);
        if (allNumber == 0) {
            resultMap.put(Constant.DEFAULT_CHARTS_PERCENT, 0);
        } else {
            resultMap.put(Constant.DEFAULT_CHARTS_PERCENT, String.format("%.1f", (number * 1.0 / allNumber) * 100) + "%");
        }
        return resultMap;
    }

    /**
     * <li>
     * <p>
     *     <strong>示例3: 返回字段、值、所占百分比(常用于扇形图)</strong> <br>
     *      dataList.add(ChartsUtils.getChartsFormatData(图表字段, 对应的值, 总数的值 浮点型)); <br>
     *      返回结果: {"name": "xx", "number": "xx", "percent": "xx"} <br>
     * </p>
     * </li>
    **/
    public static Map<String, Object> getChartsFormatData (String name, Integer number, Float allNumber) {
        Map<String, Object> resultMap = new TreeMap<>();
        resultMap.put(Constant.DEFAULT_CHARTS_NAME, name);
        resultMap.put(Constant.DEFAULT_CHARTS_NUMBER, number);
        if (allNumber == 0) {
            resultMap.put(Constant.DEFAULT_CHARTS_PERCENT, 0);
        } else {
            resultMap.put(Constant.DEFAULT_CHARTS_PERCENT, String.format("%.1f", (number * 1.0 / allNumber) * 100) + "%");
        }
        return resultMap;
    }

    /**
     * <li>
     * <p>
     *     <strong>示例4: 返回字段和自定义对象(常用于抽象结构)</strong> <br>
     *      dataList.add(ChartsUtils.getChartsFormatData(图表字段, 对应的值, 总数的值)); <br>
     *      返回结果: {"name": "xx", "list": {xxx}} <br>
     * </p>
     * </li>
     **/
    public static Map<String, Object> getChartsFormatData(String name, Object data) {
        Map<String, Object> resultMap = new TreeMap<>();
        resultMap.put(Constant.DEFAULT_CHARTS_NAME, name);
        resultMap.put("list", data);
        return resultMap;
    }
    
    /**
     * <li>
     * <p>
     *     <strong>示例5: 自定义返回字段和自定义对象(常用于抽象结构)</strong> <br>
     *      dataList.add(ChartsUtils.getChartsFormatData(图表字段, 对应的值, 总数的值)); <br>
     *      返回结果: {"xxx": "xx", "xxx": {xxx}} <br>
     * </p>
     * </li>
    **/
    public static Map<String, Object> getChartsFormatData(String keyName, Object keyValue, String keyData, Object valueData) {
        Map<String, Object> resultMap = new TreeMap<>();
        resultMap.put(keyName, keyValue);
        resultMap.put(keyData, valueData);
        return resultMap;
    }

    /**
     * <li>
     * <p>
     *     <strong>示例6: 一组返回字段和一组值 一一对应</strong> <br>
     *      dataList.add(ChartsUtils.getChartsFormatData(图表字段, 对应的key-List, 对应的value-List)); <br>
     *      返回结果: {"x1": "x1d", "x2": "x2d", "x...": "x...d"} <br>
     * </p>
     * </li>
    **/
    public static Map<String, Object> getChartsFormatData(String name, List<String> keyList, List<Integer> valueList) {
        Map<String, Object> resultMap = new TreeMap<>();
        resultMap.put(Constant.DEFAULT_CHARTS_NAME, name);
        for (int i = 0; i < keyList.size(); i ++) {
            resultMap.put(keyList.get(i), valueList.get(i));
        }
        return resultMap;
    }
}
